package com.chexiehui.jc.mvp.car;

import com.chexiehui.jc.Constants;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePage;
import com.chexiehui.jc.base.BasePresenter;
import com.chexiehui.jc.entity.ForumInfo;

/**
 * @Description 车系论坛列表
 * @Auth SongJun
 * @Date 2019/11/20 10:05
 **/
public class CarSeriesForumListPresenter extends BasePresenter<CarSeriesForumListCovenant.View, CarSeriesForumListCovenant.Stores>
        implements CarSeriesForumListCovenant.Presenter {

    public CarSeriesForumListPresenter(CarSeriesForumListCovenant.View view) {
        attachView(view);
    }

    @Override
    public void getList(int pageNum) {
        addSubscription(appStores.getList("0", mvpView.getSeriesId(), pageNum, 10),
                new ApiCallback<BaseModel<BasePage<ForumInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<BasePage<ForumInfo>> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null && model.getData().getItems() != null &&
                                    model.getData().getItems().size() > 0) {
                                mvpView.onGetListSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "没有找到任何数据");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void doFollowForum(String objId) {
        addSubscription(appStores.doFollowForum(objId, "1"),
                new ApiCallback<BaseModel<String>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<String> model) {
                        if (model.getResultCode() == 0) {
                            model.setData(objId);
                            mvpView.onDoFollowForumSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onDoFollowForumFailure(new BaseModel<>(code, msg));
                    }
                });
    }
}